package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.github.talelin.latticy.dto.zty.ShopListDTO;
import io.github.talelin.latticy.dto.zty.child.ShopListDetailDTO;
import io.github.talelin.latticy.mapper.*;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.pojo.*;
import io.github.talelin.latticy.service.WarehouseInputListService;
import io.github.talelin.latticy.vo.zty.InputDetailVO;
import io.github.talelin.latticy.vo.zty.InputListVO;
import io.github.talelin.latticy.vo.zty.PurchaseListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZTY
 * @since 2021-04-15
 */
@Service
public class WarehouseInputListServiceImpl extends ServiceImpl<WarehouseInputListMapper, WarehouseInputList> implements WarehouseInputListService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    WarehousePurchaseListMapper warehousePurchaseListMapper;

    @Autowired
    WarehouseInputDetailMapper warehouseInputDetailMapper;

    @Autowired
    WarehouseInputListMapper warehouseInputListMapper;


    @Autowired
    WarehouseReturnListMapper warehouseReturnListMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public int createPurchaseList(ShopListDTO shopListDTO) {
        WarehousePurchaseList warehousePurchaseList = new WarehousePurchaseList();
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("username", shopListDTO.getUsername());
        UserDO userDO = userMapper.selectOne(userDOQueryWrapper);
        warehousePurchaseList.setStatus(1);
        warehousePurchaseList.setSupplier(shopListDTO.getSupplierName());
        warehousePurchaseList.setUserId(userDO.getId());
        int insert = warehousePurchaseListMapper.insert(warehousePurchaseList);
        for (ShopListDetailDTO shopListDetail : shopListDTO.getShopListDetails()) {
            WarehouseInputDetail warehouseInputDetail = new WarehouseInputDetail();
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("goods_name",shopListDetail.getGoodsName());
            Goods goods = goodsMapper.selectOne(goodsQueryWrapper);
            warehouseInputDetail.setGoodsId(goods.getGoodsId());
            warehouseInputDetail.setQuantity(shopListDetail.getNumber());
            warehouseInputDetail.setWarehouseInputId(warehousePurchaseList.getWarehousePurchaseId());
            warehouseInputDetailMapper.insert(warehouseInputDetail);
        }


        return insert;
    }

    @Override
    public List<PurchaseListVO> getPurchaseLists(){
        QueryWrapper<WarehousePurchaseList> purchaseListQueryWrapper = new QueryWrapper<>();
        purchaseListQueryWrapper.orderByAsc("status").orderByDesc("create_time");
        List<WarehousePurchaseList> warehousePurchaseLists = warehousePurchaseListMapper.selectList(purchaseListQueryWrapper);
        List<PurchaseListVO> purchaseListVOs = new ArrayList<>();
        for (WarehousePurchaseList warehousePurchaseList : warehousePurchaseLists) {
            UserDO userDO = userMapper.selectById(warehousePurchaseList.getUserId());
            PurchaseListVO purchaseListVO = new PurchaseListVO();
            purchaseListVO.setCreateTime(warehousePurchaseList.getCreateTime());
            purchaseListVO.setSupplier(warehousePurchaseList.getSupplier());
            purchaseListVO.setWarehousePurchaseId(warehousePurchaseList.getWarehousePurchaseId());
            purchaseListVO.setUsername(userDO.getUsername());
            if (warehousePurchaseList.getStatus() == 1){
                purchaseListVO.setStatus("已提交，等待到货并入库");
            }else if (warehousePurchaseList.getStatus() == 2){
                purchaseListVO.setStatus("已入库");
            }else if (warehousePurchaseList.getStatus() == 3){
                purchaseListVO.setStatus("已退货");
            }else{
                purchaseListVO.setStatus("未知");
            }
            purchaseListVOs.add(purchaseListVO);
        }
        return purchaseListVOs;
    }

    @Override
    public List<InputDetailVO> getPurchaseDetailsByPurchaseListId(int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("warehouse_input_id",id);
        List<WarehouseInputDetail> warehouseInputDetails = warehouseInputDetailMapper.selectByMap(map);
        List<InputDetailVO> inputDetailVOs = new ArrayList<>();
        for (WarehouseInputDetail warehouseInputDetail : warehouseInputDetails) {
            InputDetailVO inputDetailVO = new InputDetailVO();
            Goods goods = goodsMapper.selectById(warehouseInputDetail.getGoodsId());
            inputDetailVO.setWarehouseInputId(warehouseInputDetail.getWarehouseInputId());
            inputDetailVO.setWarehouseInputDetailId(warehouseInputDetail.getWarehouseInputDetailId());
            inputDetailVO.setQuantity(warehouseInputDetail.getQuantity());
            inputDetailVO.setGoodsName(goods.getGoodsName());
            inputDetailVO.setInputPrice(goods.getInputPrice());
            inputDetailVOs.add(inputDetailVO);
        }
        return inputDetailVOs;
    }

    @Override
    public int createInputList(int purchaseListId, int handlerId) {
        WarehousePurchaseList warehousePurchaseList = warehousePurchaseListMapper.selectById(purchaseListId);
        warehousePurchaseList.setStatus(2);
        warehousePurchaseListMapper.updateById(warehousePurchaseList);
        WarehouseInputList warehouseInputList = new WarehouseInputList();
        warehouseInputList.setStatus(2);
        warehouseInputList.setUserId(handlerId);
        warehouseInputList.setWarehousePurchaseId(warehousePurchaseList.getWarehousePurchaseId());
        warehouseInputList.setSupplier(warehousePurchaseList.getSupplier());
        int insert = warehouseInputListMapper.insert(warehouseInputList);
        return insert;
    }

    @Override
    public List<InputListVO> getInputLists() {
        QueryWrapper<WarehouseInputList> inputListQueryWrapper = new QueryWrapper<>();
        inputListQueryWrapper.orderByAsc("status").orderByDesc("create_time");
        List<WarehouseInputList> warehouseInputLists = warehouseInputListMapper.selectList(inputListQueryWrapper);
        List<InputListVO> inputListVOs = new ArrayList<>();
        for (WarehouseInputList warehouseInputList : warehouseInputLists) {
            InputListVO inputListVO = new InputListVO();
            inputListVO.setCreateTime(warehouseInputList.getCreateTime());
            UserDO userDO = userMapper.selectById(warehouseInputList.getUserId());
            inputListVO.setUsername(userDO.getUsername());
            inputListVO.setWarehouseInputId(warehouseInputList.getWarehouseInputId());
            inputListVO.setSupplier(warehouseInputList.getSupplier());
            if (warehouseInputList.getStatus() == 1){
                inputListVO.setStatus("11");
            }else if (warehouseInputList.getStatus() == 2){
                inputListVO.setStatus("22");
            }else if (warehouseInputList.getStatus() == 3){
                inputListVO.setStatus("33");
            }else{
                inputListVO.setStatus("44");
            }
            inputListVOs.add(inputListVO);
        }
        return inputListVOs;
    }

    @Override
    public List<InputDetailVO> getPurchaseDetailsByInputListId(int id) {
        WarehouseInputList warehouseInputList = warehouseInputListMapper.selectById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("warehouse_input_id",warehouseInputList.getWarehousePurchaseId());
        List<WarehouseInputDetail> warehouseInputDetails = warehouseInputDetailMapper.selectByMap(map);
        List<InputDetailVO> inputDetailVOs = new ArrayList<>();
        for (WarehouseInputDetail warehouseInputDetail : warehouseInputDetails) {
            InputDetailVO inputDetailVO = new InputDetailVO();
            Goods goods = goodsMapper.selectById(warehouseInputDetail.getGoodsId());
            inputDetailVO.setWarehouseInputId(warehouseInputDetail.getWarehouseInputId());
            inputDetailVO.setWarehouseInputDetailId(warehouseInputDetail.getWarehouseInputDetailId());
            inputDetailVO.setQuantity(warehouseInputDetail.getQuantity());
            inputDetailVO.setGoodsName(goods.getGoodsName());
            inputDetailVO.setInputPrice(goods.getInputPrice());
            inputDetailVOs.add(inputDetailVO);
        }
        return inputDetailVOs;
    }

    @Override
    public int createReturnList(int inputListId, int handlerId) {
        WarehouseInputList warehouseInputList = warehouseInputListMapper.selectById(inputListId);
        warehouseInputList.setStatus(2);
        warehouseInputListMapper.updateById(warehouseInputList);
        WarehouseReturnList warehouseReturnList = new WarehouseReturnList();
        warehouseReturnList.setStatus(2);
        warehouseReturnList.setUserId(handlerId);
        warehouseReturnList.setWarehouseInputId(warehouseInputList.getWarehouseInputId());
        warehouseReturnList.setSupplier(warehouseInputList.getSupplier());
        int insert = warehouseReturnListMapper.insert(warehouseReturnList);
        return insert;
    }
}
